<?php

/**
 * 后台消息推送
 *
 * @author 周剑锋 <zf616734237@gmail.com>
 */

define('SITE_AUTH', 'mobile-app-notice');
require('../../../common.inc.php');

use lib\core\AdminAction;
use Notice\Sys;

class notice extends AdminAction
{
    private $types = [
        Sys::TYPE_MSG_ACT_OVER => '活动结果即将公布通知',
        Sys::TYPE_MSG_ACT_RESULT => '结果公示通知',
        Sys::TYPE_MSG_ACT_REWARD => '活动获奖通知',
        Sys::TYPE_MSG_INVITE => '邀请人消息',
        Sys::TYPE_MSG_SYS => '系统消息',
        Sys::TYPE_MSG_DONATE => '赠送金果',
        Sys::TYPE_MSG_COUPON => '电子券获奖通知',
    ];

    /**
     * 首页
     */
    public function doDefault()
    {
        $smarty = $this->app->page();
        $smarty->value('types', $this->types);
        $smarty->output();
    }

    /**
     * type:    通知类型
     * title:    标题
     * msg:     通知
     * url:    通知url
     *
     * 公共系统通知发送
     *
     */
    public function doSendAll()
    {
        $tid = (int)$_POST['tid'];
        $type = (int)$_POST['type'];
        $data = $_POST['data'];

        if (empty($tid) || empty($type) || empty($data)) {
            self::output(['status' => false, 'type' => 'error', 'msg' => '所有内容都不能为空']);
        } else {
            $activeid = $shopid = $itemid = $userid = 0;
            $scheme = $cont = '';
            switch ($type) {
                case Sys::TYPE_MSG_ACT_OVER:
                    $activeid = $data;
                    break;
                case Sys::TYPE_MSG_ACT_RESULT:
                    $activeid = $data;
                    $scheme = 'gold://app/activeResult?id=' . $activeid;
                    break;
                case Sys::TYPE_MSG_ACT_REWARD:
                    $activeid = $data;
                    $scheme = 'gold://app/prizeResult?id=' . $activeid;
                    break;
                case Sys::TYPE_MSG_INVITE:
                    $userid = $data;
                    break;
                case Sys::TYPE_MSG_SYS:
                    $itemid = $data;
                    break;
                case Sys::TYPE_MSG_DONATE:
                    $cont = $data;
                    $scheme = '';
                    break;
                case Sys::TYPE_MSG_COUPON:
                    $activeid = $data;
                    $scheme = 'gold://app/ticketResult?id=' . $activeid;
                    break;
            }
            $struct = [
                'tid' => $tid, // 消息分类ID
                'type' => $type, // 消息类型ID
                'activeid' => $activeid, //活动ID
                'shopid' => $shopid, //商铺ID
                'cont' => $cont, // 消息内容
                'userid' => $userid, //邀请的用户、图文消息id
                'itemid' => $itemid, //邀请的用户、图文消息id
                'url' => $scheme, // 跳转链接地址
                't' => time() // 时间戳
            ];
            $re = Sys::sendAll($tid, $struct);
            if ($re) {
                self::output(['status' => true, 'type' => 'success']);
            } else {
                self::output(['status' => true, 'type' => 'error']);
            }
        }
    }

    /**
     * type:    通知类型
     * title:    标题
     * msg:     通知
     * url:    通知url
     * uid:   用户ID
     *
     * 个人系统消息发送
     */
    public function doSendPerson()
    {
        $uid = trim($_POST['uid']);
        $tid = (int)$_POST['tid'];
        $type = (int)$_POST['type'];
        $data = $_POST['data'];

        $uids = explode(',', $uid);

        if (empty($uids)) {
            $this->output(['status' => false, 'type' => 'error', 'msg' => '请选填写有效的用户id！']);
        } elseif (empty($tid) || empty($type) || empty($data)) {
            self::output(['status' => false, 'type' => 'error', 'msg' => '所有通知内容都不能为空！']);
        } else {
            $activeid = $shopid = $itemid = $userid = 0;
            $scheme = $cont = '';
            switch ($type) {
                case Sys::TYPE_MSG_ACT_OVER:
                    $activeid = $data;
                    break;
                case Sys::TYPE_MSG_ACT_RESULT:
                    $activeid = $data;
                    $scheme = 'gold://app/activeResult?id=' . $activeid;
                    break;
                case Sys::TYPE_MSG_ACT_REWARD:
                    $activeid = $data;
                    $scheme = 'gold://app/prizeResult?id=' . $activeid;
                    break;
                case Sys::TYPE_MSG_INVITE:
                    $userid = $data;
                    break;
                case Sys::TYPE_MSG_SYS:
                    $itemid = $data;
                    break;
                case Sys::TYPE_MSG_DONATE:
                    $cont = $data;
                    $scheme = "";
                    break;
                case Sys::TYPE_MSG_COUPON:
                    $activeid = $data;
                    $scheme = 'gold://app/ticketResult?id=' . $activeid;
                    break;
            }
            $struct = [
                'tid' => $tid, // 消息分类ID
                'type' => $type, // 消息类型ID
                'activeid' => $activeid, //活动ID
                'shopid' => $shopid, //商铺ID
                'cont' => $cont ? $cont : "1", // 消息内容
                'userid' => $userid, //邀请的用户、图文消息id
                'itemid' => $itemid, //邀请的用户、图文消息id
                'url' => $scheme ? $scheme : "1", // 跳转链接地址
                't' => time() // 时间戳
            ];
            $result = Sys::addNotice($uids, $tid, $struct);
            if ($result) {
                $this->output(['status' => true, 'type' => 'success', 'msg' => '发送成功！']);
            } else {
                $this->output(['status' => false, 'type' => 'error', 'msg' => '发送失败，请稍后再试！']);
            }
        }
    }

    public function doNoticeList()
    {
        $page = (int)$_GET['page'];
        $title = trim($_GET['title']);
        $status = trim($_GET['status']);
        if ($page < 1) {
            $page = 1;
        }
        $limit = 15;
        $offset = ($page - 1) * $limit;
        $where = [];
        if (!empty($title)) {
            $title = str_replace('%', '\%', $title);
            $title = str_replace('_', '\_', $title);
            $title = str_replace('"', '\"', $title);
            $where['Title LIKE'] = '"%' . $title . '%"';
        }
        if ($status !== '') {
            $status = intval($status);
            $where['Status'] = $status;
        }
        $list_info = \Center\Notice::getList($where, $offset, $limit);
        $count = $list_info['count'];
        $smarty = $this->app->page();
        if ($count > 0) {
            $pagination = $this->pagination($count, $limit, $page);
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;
            $list = $list_info['data'];
            $smarty->value('pagination', $pagination);
            $smarty->value('page_info', $page_info);
            $smarty->value('page', $page);
            $smarty->value('list', $list);
        }

        $smarty->value('title', $title);
        $smarty->value('status', $status);

        $smarty->output('mobile/notice_list.tpl');
    }

    public function doEdit()
    {
        $id = intval($_GET['id']);
        $smarty = $this->app->page();
        if ($id) {
            $info = \Center\Notice::getInfoByIds($id);
            $smarty->value('info', $info);
        }

        self::uploadGrant();

        $smarty->output('mobile/notice_edit.tpl');
    }

    public function doUpdate()
    {
        $id = intval($_POST['Id']);
        $title = trim($_POST['Title']);
        $cont = trim($_POST['Content']);
        $data = [
            'Title' => $title,
            'Content' => $cont,
        ];
        if ($id > 0) {
            $ret = \Center\Notice::update($id, $data);
        } else {
            $ret = \Center\Notice::add($data);
        }
        if ($ret !== false) {
            HDshowMsg('编辑成功');
        } else {
            HDshowMsg('编辑失败');
        }
    }

    /**
     * 编辑器图片/视频上传
     */
    private function uploadGrant()
    {
        $_SESSION['upload_grant_name'] = 'editor';
        $base = 'editor/pic/';
        $base_video = 'editor/video/';
        if (__ENV__ == 'DEV') {
            $_SESSION['upload_file_path'] = $base;
            $_SESSION['upload_video_path'] = $base_video;
        } elseif (__ENV__ == 'TEST') {
            $_SESSION['upload_file_path'] = $base;
            $_SESSION['upload_video_path'] = $base_video;
        } else {
            $_SESSION['upload_file_path'] = $base;
            $_SESSION['upload_video_path'] = $base_video;
        }

    }
}

$app->run();
